//
//  TSLoginViewController.m
//  chart
//
//  Created by xu on 15/7/21.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSLoginViewController.h"
#import "TSRegisterViewController.h"
#import "TSHomeTabBarController.h"
#import "TSColor.h"
#import "TSImageController.h"
#import "TSLoginTextFieldView.h"
#import "TSLoginCheckBox.h"
#define MARGIN_TOP      300
#define MARGIN_TOP_LOGIN      14
#define MARGIN_LEFT_LOGIN     44
#define FLD_HEIGHT      41
#define BTN_HEIGHT      56

#define TXT_COLOR       RGB(168, 168, 168)
#define BTN_COLOR       RGB(107, 186, 81)

@interface TSLoginViewController ()<UITextFieldDelegate>{
    TSLoginTextFieldView             *phoneNumField;
    TSLoginTextFieldView             *passwordField;
    UIButton                *loginBtn;
    UIButton                *registerBtn;
    
    TSLoginCheckBox * rememberCheckBox;
    TSLoginCheckBox * autoLoginCheckBox;
}

@end

@implementation TSLoginViewController

 - (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"青山政务";
    UIImage*draw = [UIImage imageNamed:@"login_bg"];
    UIImageView *drawView = [[UIImageView alloc]initWithImage:draw];
    [drawView setFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:drawView];
    [self leftBarButtonWithTitle:nil barNorImage:nil barHltImage:nil action:nil];
    /* 加入所有控件 */
    [self addUIComponent];
     
    if([USERDEFAULTS boolForKey:TSUserDefaultsKey_AutoLogin]){
        [self loginButtonClicked:nil];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=YES;
}

- (void)addUIComponent{
    
    /* 加入手机号和密码输入框 */
    [self addPhoneAndPwdView];
    /*添加checkbox*/
    [self addCheckBox];
    /* 加入登录按钮 */
    [self addLoginBtn];
    /* 加入注册按钮 */
    [self addRegisterBtn];
    /* 加入收键盘手势 */
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleField:)];
    [self.view addGestureRecognizer:tap];
}

- (void)handleField:(UIGestureRecognizer*)gesture{
    
    for (UIView * view in self.view.subviews) {
        if ([view isFirstResponder]) {
            [view resignFirstResponder];
        }
    }
}

/* 加入手机号和密码输入框 */
- (void)addPhoneAndPwdView
{
    
    phoneNumField = [[TSLoginTextFieldView alloc] init];
    phoneNumField.fieldType = LoginTextField_USER_NAME;
    [phoneNumField initUI];
    [self.view addSubview:phoneNumField];
    
    passwordField = [[TSLoginTextFieldView alloc] init];
    passwordField.fieldType = LoginTextField_PASSWORD;
    [passwordField initUI];
    [self.view addSubview:passwordField];
}

-(void)addCheckBox{
    rememberCheckBox = [[TSLoginCheckBox alloc] init];
    rememberCheckBox.type = LoginCheckBox_Remember;
    [rememberCheckBox initUI];
    [self.view addSubview:rememberCheckBox];
    
    autoLoginCheckBox = [[TSLoginCheckBox alloc] init];
    autoLoginCheckBox.type = LoginCheckBox_AutoLogin;
    [autoLoginCheckBox initUI];
    [self.view addSubview:autoLoginCheckBox];
}

/* 加入登录按钮 */
- (void)addLoginBtn
{
    loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    loginBtn.titleLabel.font = FONT(12);
    [loginBtn setTitle:@"进入系统" forState:UIControlStateNormal];
    [loginBtn setBackgroundColor:RGBA(112, 220, 255, 0.7)];
    [loginBtn.layer setCornerRadius:5.f];
    [loginBtn addTarget:self action:@selector(loginButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
}

/* 加入注册按钮 */
- (void)addRegisterBtn
{
    registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    registerBtn.titleLabel.font = FONT(12);
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registerBtn setBackgroundColor:[UIColor grayColor]];
    [registerBtn.layer setCornerRadius:5.f];
    [registerBtn addTarget:self action:@selector(registerUser:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerBtn];
}

/* 右上角按钮点击注册 */
- (void)registerUser:(UIButton *)button
{
    [[LBToastManager manager] showtoast:@"内部系统不允许外部注册"];
    return;
    
    //    TSRegisterViewController *register_vc = [[TSRegisterViewController alloc] init];
    //    [self.navigationController pushViewController:register_vc animated:YES];
}

- (void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
    
    phoneNumField.frame = CGRectMake(MARGIN_LEFT_LOGIN,
                                     MARGIN_TOP_LOGIN+MARGIN_TOP,
                                     CGRectGetWidth(self.view.frame)-2*MARGIN_LEFT_LOGIN,
                                     FLD_HEIGHT);
    passwordField.frame = CGRectMake(MARGIN_LEFT_LOGIN,MARGIN_TOP+
                                     MARGIN_TOP_LOGIN*2+FLD_HEIGHT,
                                     CGRectGetWidth(phoneNumField.frame),
                                     FLD_HEIGHT);
    
    rememberCheckBox.frame =CGRectMake(MARGIN_LEFT_LOGIN, MARGIN_TOP+(MARGIN_TOP_LOGIN+FLD_HEIGHT)*2+MARGIN_TOP_LOGIN, 50, 10);
    autoLoginCheckBox.frame =CGRectMake(currentDeviceWidth-MARGIN_LEFT_LOGIN-55, MARGIN_TOP+(MARGIN_TOP_LOGIN+FLD_HEIGHT)*2+MARGIN_TOP_LOGIN,50, 10);
    
    loginBtn.frame = CGRectMake(MARGIN_LEFT_LOGIN, MARGIN_TOP+(MARGIN_TOP_LOGIN+FLD_HEIGHT)*2+MARGIN_TOP_LOGIN+25, CGRectGetWidth(passwordField.frame), FLD_HEIGHT);
    
    
   
}

/* login click */
- (void)loginButtonClicked:(UIButton*)sender{
    WS(weakSelf);
    [[TSUserModel sharedInstance] loginWithUser:phoneNumField.text andPassword:passwordField.text completionHandler:^(BOOL isSucceeded, NSError *error) {
        if (!weakSelf) {
            return;
        }
        
        if (isSucceeded) {
            TSImageController * home = [[TSImageController alloc] init];
            [self presentViewController:home animated:YES completion:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:TSNotification_HasLogined object:nil];
        }
    }];
}



- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
}

- (void)dealloc
{
    
}

@end
